package client;

import java.rmi.RemoteException;
import java.util.ArrayList;

import javax.swing.JDialog;

import clientUI.SharePromtDialog;

import shared.MasterServerInterface;
import shared.*;

public class ShareHandler implements Runnable{
	
	private Client client;
	
	ShareHandler(Client client){
		this.client = client;
	}
	
	
	public void requestShare (long fileID, String username)throws RemoteException {
	
			client.getMasterServer().requestShare(fileID, username);
	}
	
	public void accept(ShareInvite invite)throws Exception{
		client.getMasterServer().confirmShare(invite.getFileID(),client.currentUser.getUsername());
	}
	
	public void decline(ShareInvite invite) throws Exception{
		//TODO
		
	}

	@Override
	public void run() {
		
		while (true){
			ArrayList<ShareInvite> invites = client.currentUser.getMyInvites();
			
			for (ShareInvite invite: invites){
				SharePromtDialog dialog = new SharePromtDialog(invite, this);
				dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
				dialog.setVisible(true);
				
				invites.remove(invite);
			}
		
			
			try {
				Thread.sleep(Configuration.SHARE_CHECK_PERIOD);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
}
